﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using Alexim.Model;
using Alexim.BL;
using System.Messaging;
using System.Linq;
using System.Runtime.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Microsoft.Win32;
using Apache.NMS;
using Apache.NMS.ActiveMQ;



namespace RelateImageDocALS
{
    public class RelateImageComm
    {
        // const string strCola = @".\private$\colaBecas";
        private const string URI = "tcp://localhost:61616";
        private const string DESTINATION = "colaBecas";

        SendMail objSendMail = new SendMail();


        protected internal String _serverSMTP;
        public String serverSMTP
        {
            get { return _serverSMTP; }
            set { _serverSMTP = value; }
        }

        protected internal String _userSMTP;
        public String userSMTP
        {
            get { return _userSMTP; }
            set { _userSMTP = value; }
        }

        protected internal String _passwordSMTP;
        public String passwordSMTP
        {
            get { return _passwordSMTP; }
            set { _passwordSMTP = value; }
        }

        protected internal String _fromMail;
        public String fromMail
        {
            get { return _fromMail; }
            set { _fromMail = value; }
        }

        protected internal String _toMailSystems;
        public String toMailSystems
        {
            get { return _toMailSystems; }
            set { _toMailSystems = value; }
        }



        public RelateImageComm()
        {

            this._serverSMTP = "smtp.gmail.com ";
            this._userSMTP = "upcgrupoidstribuidos02@gmail.com";
            this._passwordSMTP = "upcgrupoidstribuidos";
            this._fromMail = "upcgrupoidstribuidos02@gmail.com";
        }



        public void DocumentRelated()
        {
            // MessageQueue msMq = msMq = new MessageQueue(strCola);

            IConnectionFactory connectionFactory = new ConnectionFactory(URI);

            IConnection conn = connectionFactory.CreateConnection();
            conn.Start();

            ISession session = conn.CreateSession();

            IDestination dest = session.GetQueue(DESTINATION);

            using (IMessageConsumer consumer = session.CreateConsumer(dest))
            {
                ITextMessage message;

                while ((message = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage) != null)
                {

                    if (message != null)
                    {
                        Console.WriteLine(message.Text);

                        string[] token = message.Text.Split(';');

                        Alexim.BL.BecaBL objLT = new Alexim.BL.BecaBL();
                        List<Beca> lBeca = new List<Beca>();
                        lBeca = objLT.getBecaInformation(int.Parse(token[1]), int.Parse(token[2]));

                        SendMail objMail = new SendMail();


                        string Cuerpo = string.Empty;
                        int contador = 0;

                        foreach (var item in lBeca)
                        {
                            contador++;
                            Cuerpo = Cuerpo + "Opcion " + contador.ToString() + System.Environment.NewLine;
                            Cuerpo = Cuerpo + "Carrera :" + item.Carrera + System.Environment.NewLine;
                            Cuerpo = Cuerpo + "Universidad :" + item.Universidad + System.Environment.NewLine;
                            Cuerpo = Cuerpo + "Fecha inicio :" + item.FechaInicio + System.Environment.NewLine + System.Environment.NewLine;

                        }

                        objMail.EnviarMail(token[3], this.fromMail, "Informacion sobre Beca solicitada", Cuerpo, this.serverSMTP, this.userSMTP, this.passwordSMTP);

                    }
                }
            }

            


            //try
            //{
            //    if (!MessageQueue.Exists(strCola))
            //        MessageQueue.Create(strCola);

            //    MessageQueue cola = new MessageQueue(strCola);
            //    Message[] mensaje = cola.GetAllMessages();

            //    foreach (var men in mensaje)
            //    {
            //        cola.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            //        Message m = cola.Receive();
            //        string opcion = (string)m.Body;
            //        string[] CriterioBuscqueda = opcion.Split(';');


            //        Alexim.BL.BecaBL objLT = new Alexim.BL.BecaBL();
            //        List<Beca> lBeca = new List<Beca>();
            //        lBeca = objLT.getBecaInformation(int.Parse(CriterioBuscqueda[1]), int.Parse(CriterioBuscqueda[2]));

            //        SendMail objMail = new SendMail();


            //        string Cuerpo = string.Empty;
            //        int contador = 0;

            //        foreach (var item in lBeca)
            //        {
            //            contador++;
            //            Cuerpo = Cuerpo + "Opcion " + contador.ToString() + System.Environment.NewLine;
            //            Cuerpo = Cuerpo + "Carrera :" + item.Carrera + System.Environment.NewLine;
            //            Cuerpo = Cuerpo + "Universidad :" + item.Universidad + System.Environment.NewLine;
            //            Cuerpo = Cuerpo + "Fecha inicio :" + item.FechaInicio + System.Environment.NewLine + System.Environment.NewLine;

            //        }

            //        objMail.EnviarMail(CriterioBuscqueda[3], this.fromMail, "Informacion sobre Beca solicitada", Cuerpo, this.serverSMTP, this.userSMTP, this.passwordSMTP);
            //    }

            //}
            //catch (MessageQueueException ee)
            //{
            //    throw ee;
            //}
            //catch (Exception eee)
            //{
            //    throw eee;
            //}
            //finally
            //{
            //    msMq.Close();
            //}

        }



        //public void RegistraVoto(string _Beca)
        //{
        //    if (!MessageQueue.Exists(strCola))
        //        MessageQueue.Create(strCola);
        //    MessageQueue cola = new MessageQueue(strCola);

        //    Message mensaje = new Message();
        //    mensaje.Body = _Beca;
        //    cola.Send(mensaje);
        //}




    }
}
